<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="__ROOT__/Public/Admin/css/shortcut icon" href="favicon.ico">
    <link href="__ROOT__/Public/Admin/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="__ROOT__/Public/Admin/css/animate.css" rel="stylesheet">
    <link href="__ROOT__/Public/Admin/css/style.css?v=4.1.0" rel="stylesheet">
    <link href="__ROOT__/Public/Admin/css/default.css" rel="stylesheet">
    <link href="__ROOT__/Public/Admin/js/plugins/sweetalert/sweetalert.css" rel="stylesheet">
    <link rel="stylesheet" href="__ROOT__/Public/Admin/js/AuthorityTree/dtree.css" type="text/css" />
    <script type="text/javascript" src="__ROOT__/Public/Admin/js/AuthorityTree/dtree.js"></script>
    <script>
        var CC = {
            "PUBLIC":"__PUBLIC__",
            "ROOT":"__ROOT__",
            "APP":"__APP__",
        };
    </script>

    <style>
        .nav-tabs{
            margin-bottom: 20px;
        }
        .err-tip-color{
            color:#ed2a2a;
        }
        #content-main{height: calc(100%) !important;}
    </style>
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content  animated fadeInRight">
        <div class="row">
             <div class="ibox" style="min-height: 500px">
                <div class="ibox-content">
                        <ul class="nav nav-tabs">
                            <li class="active"><a data-toggle="tab" href="#tab-1"><i class="fa fa-Role"></i>{$Role.Name} 角色授权</a>
                            </li>
                            
                        </ul>
                        
                        <div class="clients-list">
                            <p><a href="javascript:d.closeAll();">展开全部</a> | <a href="javascript:d.openAll();">关闭全部</a></p>
                            <div class="tab-content">
                                    <div class="full-height-scroll dtree">
                                        <input type="hidden" id="RoleID" value="{$_GET['id']}">
                                            <script type="text/javascript">
                                                <if condition="$AuthRules">  
                                                    var authJson = JSON.parse('{$AuthRules}');
                                                    <else />
                                                    var authJson = {};
                                                </if> 
                                                // dTree实例属性以此为：  节点ID，父类ID，chechbox的名称，chechbox的值，chechbox的显示名称，chechbox是否被选中--默认是不选，chechbox是否可用：默认是可用，节点链接：默认是虚链接
                                                //d.add(6,0,'authority','0012','一级菜单2 ',true,false);
                                                var d = new dTree('d');
                                                d.add(0,-1,'全部权限');
                                                for(var i in authJson){
                                                    var checked = false;
                                                    if(authJson[i].Checked){
                                                        checked = true;
                                                    }
                                                    d.add(parseInt(authJson[i].ID),parseInt(authJson[i].ParentID),'authority',authJson[i].Name,authJson[i].Title,checked);//第一级
                                                    if(authJson[i].Son){
                                                        var subAuthTow = authJson[i].Son;
                                                        for(var o in subAuthTow){
                                                            var checked = false;
                                                            if(subAuthTow[o].Checked){
                                                                checked = true;
                                                            }
                                                            d.add(parseInt(subAuthTow[o].ID),parseInt(subAuthTow[o].ParentID),'authority',subAuthTow[o].Name,subAuthTow[o].Title,checked);
                                                            if(subAuthTow[o].Son){
                                                                var subAuthThree = subAuthTow[o].Son;
                                                                for(var n in subAuthThree){
                                                                    var checked = false;
                                                                    if(subAuthThree[n].Checked){
                                                                        checked = true;
                                                                    }
                                                                    d.add(parseInt(subAuthThree[n].ID),parseInt(subAuthThree[n].ParentID),'authority',subAuthThree[n].Name,subAuthThree[n].Title,checked);
                                                                    if(subAuthThree[n].Son){
                                                                        var subAuthFour = subAuthThree[n].Son;
                                                                        for(var f in subAuthFour){
                                                                            var checked = false;
                                                                            if(subAuthFour[f].Checked){
                                                                                checked = true;
                                                                            }
                                                                            d.add(parseInt(subAuthFour[f].ID),parseInt(subAuthFour[f].ParentID),'authority',subAuthFour[f].Name,subAuthFour[f].Title,checked);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                document.write(d);
                                                //d.openAll();
                                                /*function appendTree(){
                                                    d.openTo();
                                                }*/
                                            </script>
                                    </div>
                            </div>
                            
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-8">
                            <button class="btn btn-primary" id="submit" type="button" onclick='submit();'>保存</button>
                            <a class="btn btn-white" href="javascript:void(0);" onclick="self.location=document.referrer;">返回</a>
                        </div>
                    </div>
                </div>
        </div>
    </div>
    <!-- 全局js -->
    <script>
        var GV = {
            ROOT: "__ROOT__/",
            WEB_ROOT: "__ROOT__/",
            JS_ROOT: "Public/Admin/js/"
        };
    </script>
    <script type="text/javascript" src="__ROOT__/Public/Admin/js/jquery.min.js"></script>
    <script type="text/javascript" src="__ROOT__/Public/Admin/js/wind.js"></script>
    <script type="text/javascript" src="__ROOT__/Public/Admin/js/common.js"></script>
    <script src="__ROOT__/Public/Admin/js/plugins/sweetalert/sweetalert.min.js"></script>
    <!-- 自定义js -->
    <script>
        var formloading = false;
        function submit(){
            var obj = document.all.authority; 
            var AuthRules = [];
            for(i=0;i<obj.length;i++){
                if(obj[i].checked){                 
                    //console.log(obj[i].value);
                    AuthRules.push(obj[i].value);             
                }
                //console.log(i+'>='+(obj.length-1))
                if(i>=(obj.length-1)){
                    //防止异步
                    post(AuthRules);
                }
            }
        }
        function post(data){
            if (formloading)
                return;
            $.ajax({
                url : "{:U('Admin/Auth/authorize')}", //按钮上是否自定义提交地址(多按钮情况)
                type:'POST', //GET
                timeout:10000,  //超时时间
                data:{AuthRule:data,RoleID:$('#RoleID').val()},
                dataType : 'json',
                beforeSubmit : function(arr, $form, options) {
                    $('#submit').html('请稍候…');
                    formloading = true;
                },
                success : function(data, statusText, xhr, $form) {
                    formloading = false;
                    if (data.status) {
                        //成功
                        swal({
                            title: "操作成功",
                            text:  data.info,
                            type: "success"
                        },
                        function () {
                            document.location.href = "{:U('Admin/Role/index')}";
                            return true;
                        });
                    } else {
                        swal("操作失败", data.info, "error");
                    }
                },
                error:function(xhr,textStatus){
                   alert('网络异常');
                   $('#submit').html('提交');
                },
                complete:function(){
                    formloading = false;
                    $('#submit').html('提交');
                }
            });
        }
       
    </script>
</body>

</html>
